﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SkillManager : MonoBehaviour
{
    /// <summary> Skill对象 </summary>
    public List<Skill> Skills;

    /// <summary> Skill对象模板 </summary>
    List<Skill> SkillsModel;

    /// <summary> UI界面的3个button </summary>
    public SkillButton[] skillButtons;

    /// <summary> 判断获得几次技能 </summary>
    public float skillCount = 0;

    public static SkillManager _instance;


    void Awake()
    {
        _instance = this;
        if (Skills.Count != 0)
            SkillsModel = Skills;//存储技能库
    }

    void Start()
    {
        if (SkillsModel.Count != Skills.Count)
            Skills = SkillsModel;//还原技能库
    }

    /// <summary> 获得技能 </summary>
    public void gainSkill(GameObject go)
    {   
        if (skillCount > 0)
        {
            skillCount += 1;
            return;
        }
        else
        {
            skillCount += 1;
            ShowButton();
            ini(go);
        }
    }

    public List<int> temp;//用来存储游戏中实例化时生成按钮对应的技能
    /// <summary> 初始化随机按钮 </summary>
    public void ini(GameObject go)
    {
        for (int i = 0; i < skillButtons.Length; i++)
        {
            //获取可复用技能
            skillButtons[i].initializeButton(getRandomSkill(), go);
        }
        temp.Clear();
    }

    /// <summary> 每个按钮获得一个随机技能 </summary>
    /// <returns> 每次循环返回单个技能对象 </returns>
    public Skill getRandomSkill()
    {
        while (true)
        {
            int r = Random.Range(0, Skills.Count);

            if (r <= Skills.Count - 1 && !temp.Contains(r))
            {
                temp.Add(r);
                return Skills[r];
           }

        }
    }
    public Skill EnemygetRandomSkill()
    {    
            int r = Random.Range(0, Skills.Count);
             return Skills[r];
        
    }

    /// <summary> 隐藏随机技能按钮 </summary>
    public void HideButton(GameObject go)
    {

        for (int i = 0; i < skillButtons.Length; i++)
        {
            skillButtons[i].gameObject.SetActive(false);

        }

        StartCoroutine(ButtonReActive(go));
    }

    /// <summary> 显示随机技能按钮 </summary>
    public void ShowButton()
    {
        for (int i = 0; i < skillButtons.Length; i++)
        {
            skillButtons[i].gameObject.SetActive(true);
        }

    }
    /// <summary>
    ///  按钮重新初始化
    /// </summary>
    IEnumerator ButtonReActive(GameObject go)
    {
        yield return new WaitForSeconds(0.01f);
        skillCount -= 1;
        if (skillCount > 0)
        {
            ShowButton();
            ini(go);
        }
    }
    public Skill getRandomSkill( GameObject go)
    {
         int r = Random.Range(0, Skills.Count);
         return Skills[r];   

    }
}
